package com.katsu.myviewlib.view

import android.content.Context
import android.os.Build
import android.util.AttributeSet
import android.view.LayoutInflater
import android.view.View
import android.widget.LinearLayout
import androidx.annotation.RequiresApi
import com.katsu.myviewlib.R
import com.katsu.myviewlib.callback.OnSwitchCallBack
import com.katsu.myviewlib.databinding.ViewMySwitchLayoutBinding
import com.katsu.myviewlib.utils.TextUtil


/**
 * Author:雅典娜
 * Created on 2024/8/20
 * 别总是抱怨生活不够幸运，是你欠了生活一份努力，
 * 每一个你讨厌的现在，都有一个不够努力的曾经，
 * 未来美不美，取决于你现在拼不拼。
 * Desc:带文字的开关
 * 后台:
 */
class MySwitchView : LinearLayout {
    private var mContext: Context

    //</editor-fold>
    var view: View? = null
    var normalColor = 0
    var selectedColor = 0
    var switchTextSize = 20
    var enableType = 1

    lateinit var binding: ViewMySwitchLayoutBinding


    constructor(context: Context) : super(context) {
        mContext = context
    }

    constructor(context: Context, attrs: AttributeSet?) : super(context, attrs) {
        mContext = context
        initViews(attrs)
    }

    constructor(context: Context, attrs: AttributeSet?, defStyleAttr: Int) : super(context, attrs, defStyleAttr) {
        mContext = context
        initViews(attrs)
    }

    //<editor-fold desc="初始化">
    private fun initViews(attrs: AttributeSet?) {
        val layoutInflater = mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE) as LayoutInflater
        val view = layoutInflater.inflate(R.layout.view_my_switch_layout, this, true)
        binding = ViewMySwitchLayoutBinding.bind(view)
        val typedArray = mContext.obtainStyledAttributes(attrs, R.styleable.MySwitchView)
        val switchLeftStr = typedArray.getString(R.styleable.MySwitchView_switchLeftStr)
        val switchRightStr = typedArray.getString(R.styleable.MySwitchView_switchRightStr)
        normalColor = typedArray.getInt(R.styleable.MySwitchView_switchTextNormalColor, 0)
        selectedColor = typedArray.getInt(R.styleable.MySwitchView_switchTextSelectedColor, 0)
        switchTextSize = typedArray.getInt(R.styleable.MySwitchView_switchTextSize, 20)
        val switchPaddingTop = typedArray.getInt(R.styleable.MySwitchView_switchPaddingTop, 0)
        val switchPaddingBottom = typedArray.getInt(R.styleable.MySwitchView_switchPaddingBottom, 0)
        val switchPaddingLeft = typedArray.getInt(R.styleable.MySwitchView_switchPaddingLeft, 0)
        val switchPaddingRight = typedArray.getInt(R.styleable.MySwitchView_switchPaddingRight, 0)
        val isChecked = typedArray.getBoolean(R.styleable.MySwitchView_switchChecked, false)
        /** isController : 是否是控制器-如果是控制器点击切换是需要串口，这里判断了如果控制器通信失效的提示不可以点击*/
        val isController = typedArray.getBoolean(R.styleable.MySwitchView_switchIsController, false)
        if (normalColor == 0) {
            normalColor = context.getColor(R.color.theme_switch_color)
        }
        if (selectedColor == 0) {
            selectedColor = context.getColor(R.color.text_purple)
        }
        setLeftStr(switchLeftStr)
        setRightStr(switchRightStr)
        binding.mySwitchCheckbox.isChecked = isChecked
        if (switchTextSize != 20) {
            binding.mySwitchLeftTv.textSize = switchTextSize.toFloat()
            binding.mySwitchLeftTv.setPadding(
                TextUtil.dp2px(context, switchPaddingLeft.toFloat()),
                TextUtil.dp2px(context, switchPaddingTop.toFloat()),
                TextUtil.dp2px(context, switchPaddingRight.toFloat()),
                TextUtil.dp2px(context, switchPaddingBottom.toFloat())
            )
        }

        setCheckedView(isChecked)
        binding.mySwitchCheckbox.setOnCheckedChangeListener { buttonView, isChecked ->


            if (enableType == 1) {
                setCheckedView(isChecked)
            } else {
                binding.mySwitchCheckbox.isChecked = !isChecked
            }

            if (callback != null) {
                callback!!.onChecked(binding.mySwitchCheckbox.isChecked, enableType)
            }
        }
        typedArray.recycle()
    }

    //</editor-fold>


    //<editor-fold desc="设置文字title">
    fun setLeftStr(str: String?) {
        binding.mySwitchLeftTv.text = TextUtil.setTextStr(str)
        binding.mySwitchLeftTv.visibility = if (TextUtil.isNotNull(str)) VISIBLE else GONE
    }

    //</editor-fold>
    //<editor-fold desc="设置文字title">
    fun setRightStr(str: String?) {
        binding.mySwitchRightTv.text = TextUtil.setTextStr(str)
        binding.mySwitchRightTv.visibility = if (TextUtil.isNotNull(str)) VISIBLE else GONE
    }
    //</editor-fold>

    fun setCheckedView(isChecked: Boolean) {

        if (!isChecked) {
            if (normalColor != 0) {
                binding.mySwitchLeftTv.setTextColor(normalColor)
                binding.mySwitchRightTv.setTextColor(normalColor)
            }
        } else {
            if (selectedColor != 0) {
                binding.mySwitchLeftTv.setTextColor(selectedColor)
                binding.mySwitchRightTv.setTextColor(selectedColor)
            }
        }

    }

    fun isChecked(): Boolean {
        return binding.mySwitchCheckbox.isChecked
    }

    fun setChecked(isChecked: Boolean) {
        binding.mySwitchCheckbox.isChecked = isChecked
    }


    private var callback: OnSwitchCallBack? = null

    fun setOnCheckedChangeListener(callBack: OnSwitchCallBack?) {
        this.callback = callBack

    }


    fun setEnableType(enableClick: Boolean?) {
        enableType = if (enableClick!!) 1 else 0
    }

}